<!DOCTYPE html>
        <html>
        <head>
            <meta charset="UTF-8">
            <title>&#x534e;&#x7530;&#x5c4f;&#x5e55;GPIO&#x64cd;&#x4f5c;&#x8bf4;&#x660e;&#x6587;&#x6863;</title>
            <style>
/* From extension vscode.github */
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

.vscode-dark img[src$=\#gh-light-mode-only],
.vscode-light img[src$=\#gh-dark-mode-only],
.vscode-high-contrast:not(.vscode-high-contrast-light) img[src$=\#gh-light-mode-only],
.vscode-high-contrast-light img[src$=\#gh-dark-mode-only] {
	display: none;
}

</style>
            
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Microsoft/vscode/extensions/markdown-language-features/media/markdown.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Microsoft/vscode/extensions/markdown-language-features/media/highlight.css">
<style>
            body {
                font-family: -apple-system, BlinkMacSystemFont, 'Segoe WPC', 'Segoe UI', system-ui, 'Ubuntu', 'Droid Sans', sans-serif;
                font-size: 14px;
                line-height: 1.6;
            }
        </style>
        <style>
.task-list-item {
    list-style-type: none;
}

.task-list-item-checkbox {
    margin-left: -20px;
    vertical-align: middle;
    pointer-events: none;
}
</style>
<style>
:root {
  --color-note: #0969da;
  --color-tip: #1a7f37;
  --color-warning: #9a6700;
  --color-severe: #bc4c00;
  --color-caution: #d1242f;
  --color-important: #8250df;
}

</style>
<style>
@media (prefers-color-scheme: dark) {
  :root {
    --color-note: #2f81f7;
    --color-tip: #3fb950;
    --color-warning: #d29922;
    --color-severe: #db6d28;
    --color-caution: #f85149;
    --color-important: #a371f7;
  }
}

</style>
<style>
.markdown-alert {
  padding: 0.5rem 1rem;
  margin-bottom: 16px;
  color: inherit;
  border-left: .25em solid #888;
}

.markdown-alert>:first-child {
  margin-top: 0
}

.markdown-alert>:last-child {
  margin-bottom: 0
}

.markdown-alert .markdown-alert-title {
  display: flex;
  font-weight: 500;
  align-items: center;
  line-height: 1
}

.markdown-alert .markdown-alert-title .octicon {
  margin-right: 0.5rem;
  display: inline-block;
  overflow: visible !important;
  vertical-align: text-bottom;
  fill: currentColor;
}

.markdown-alert.markdown-alert-note {
  border-left-color: var(--color-note);
}

.markdown-alert.markdown-alert-note .markdown-alert-title {
  color: var(--color-note);
}

.markdown-alert.markdown-alert-important {
  border-left-color: var(--color-important);
}

.markdown-alert.markdown-alert-important .markdown-alert-title {
  color: var(--color-important);
}

.markdown-alert.markdown-alert-warning {
  border-left-color: var(--color-warning);
}

.markdown-alert.markdown-alert-warning .markdown-alert-title {
  color: var(--color-warning);
}

.markdown-alert.markdown-alert-tip {
  border-left-color: var(--color-tip);
}

.markdown-alert.markdown-alert-tip .markdown-alert-title {
  color: var(--color-tip);
}

.markdown-alert.markdown-alert-caution {
  border-left-color: var(--color-caution);
}

.markdown-alert.markdown-alert-caution .markdown-alert-title {
  color: var(--color-caution);
}

</style>
        
        </head>
        <body class="vscode-body vscode-light">
            <h1 id="华田屏幕gpio操作说明文档">华田屏幕GPIO操作说明文档</h1>
<h2 id="1-系统概述">1. 系统概述</h2>
<p>华田屏幕GPIO控制系统是一套用于控制华田显示设备GPIO接口的软件库，提供了对GPIO输入输出通道的统一管理和操作。该系统采用面向对象的设计方法，通过工厂模式创建GPIO对象，实现了对不同型号设备GPIO接口的统一访问。</p>
<h3 id="11-系统架构">1.1 系统架构</h3>
<p>系统采用三层架构设计：</p>
<ul>
<li><strong>基础接口层</strong>：定义了GPIO操作的基本接口（BasicGpio类）</li>
<li><strong>实现层</strong>：实现了GPIO的具体操作（GpioController类）</li>
<li><strong>工厂层</strong>：提供了GPIO对象的创建方法（GpioFactory类）</li>
</ul>
<h3 id="12-类关系图">1.2 类关系图</h3>
<pre><code>+----------------+      +-------------------+      +----------------+
|   BasicGpio    |&lt;---- |  GpioController  |&lt;---- |  GpioFactory   |
+----------------+      +-------------------+      +----------------+
| + init()       |      | - gpioNumber      |      | + createGpio() |
| + setValue()   |      | - gpioDirection   |      | + createDI()   |
| + getValue()   |      | + init()          |      | + createDO()   |
+----------------+      | + setValue()      |      +----------------+
                        | + getValue()      |
                        +-------------------+
</code></pre>
<h2 id="2-类详细说明">2. 类详细说明</h2>
<h3 id="21-basicgpio-类">2.1 BasicGpio 类</h3>
<p>BasicGpio类是GPIO操作的基类，定义了GPIO操作的基本接口。</p>
<h4 id="主要接口">主要接口</h4>
<ul>
<li>
<p><strong>init()</strong>：初始化GPIO</p>
<ul>
<li>返回值：初始化成功返回true，失败返回false</li>
</ul>
</li>
<li>
<p><strong>setValue(int value)</strong>：设置GPIO值</p>
<ul>
<li>参数：value - 0或1</li>
<li>返回值：设置成功返回true，失败返回false</li>
</ul>
</li>
<li>
<p><strong>getValue(int&amp; value)</strong>：获取GPIO值</p>
<ul>
<li>参数：value - 用于存储获取到的值（0或1）</li>
<li>返回值：获取成功返回true，失败返回false</li>
</ul>
</li>
</ul>
<h3 id="22-gpiocontroller-类">2.2 GpioController 类</h3>
<p>GpioController类是BasicGpio的具体实现类，实现了对GPIO的具体操作。</p>
<h4 id="主要属性">主要属性</h4>
<ul>
<li><strong>gpioNumber</strong>：GPIO编号</li>
<li><strong>gpioDirection</strong>：GPIO方向（&quot;in&quot;或&quot;out&quot;）</li>
</ul>
<h4 id="构造函数">构造函数</h4>
<ul>
<li>
<p><strong>GpioController(std::string name)</strong>：通过设备路径名称创建GPIO控制器</p>
<ul>
<li>参数：name - 设备路径名称，如&quot;/dev/gpio/DI-00&quot;</li>
</ul>
</li>
<li>
<p><strong>GpioController(int number, const std::string&amp; direction)</strong>：通过GPIO编号和方向创建GPIO控制器</p>
<ul>
<li>参数：
<ul>
<li>number - GPIO编号</li>
<li>direction - GPIO方向（&quot;in&quot;或&quot;out&quot;）</li>
</ul>
</li>
</ul>
</li>
</ul>
<h4 id="主要方法">主要方法</h4>
<ul>
<li><strong>init()</strong>：初始化GPIO，包括导出GPIO、设置GPIO方向等操作</li>
<li><strong>setValue(int value)</strong>：设置GPIO值</li>
<li><strong>getValue(int&amp; value)</strong>：获取GPIO值</li>
<li><strong>fileExists(const std::string&amp; path)</strong>：检查文件是否存在</li>
<li><strong>writeToFile(const std::string&amp; path, const std::string&amp; content)</strong>：写入内容到文件</li>
<li><strong>readFromFile(const std::string&amp; path, std::string&amp; content)</strong>：读取文件内容</li>
<li><strong>resolveSymlink(const std::string&amp; path)</strong>：解析软链接的真实路径</li>
<li><strong>extractGpioNumber(const std::string&amp; path)</strong>：从路径中提取GPIO编号</li>
<li><strong>extractGpioDirection(const std::string&amp; name)</strong>：从名称中提取GPIO方向</li>
</ul>
<h3 id="23-gpiofactory-类">2.3 GpioFactory 类</h3>
<p>GpioFactory类是一个工厂类，用于创建GPIO对象。</p>
<h4 id="主要方法-1">主要方法</h4>
<ul>
<li>
<p><strong>createGpio(DIChannel channel)</strong>：通过DI通道枚举创建GPIO对象</p>
<ul>
<li>参数：channel - DI通道枚举</li>
<li>返回值：创建的GPIO对象指针</li>
</ul>
</li>
<li>
<p><strong>createGpio(int number, std::string direction)</strong>：通过GPIO编号和方向创建GPIO对象</p>
<ul>
<li>参数：
<ul>
<li>number - GPIO编号</li>
<li>direction - GPIO方向（&quot;in&quot;或&quot;out&quot;）</li>
</ul>
</li>
<li>返回值：创建的GPIO对象指针</li>
</ul>
</li>
<li>
<p><strong>createDI(DIChannel channel)</strong>：创建输入GPIO对象</p>
<ul>
<li>参数：channel - DI通道枚举</li>
<li>返回值：创建的GPIO对象指针</li>
</ul>
</li>
<li>
<p><strong>createDO(DOChannel channel)</strong>：创建输出GPIO对象</p>
<ul>
<li>参数：channel - DO通道枚举</li>
<li>返回值：创建的GPIO对象指针</li>
</ul>
</li>
</ul>
<h2 id="3-枚举说明">3. 枚举说明</h2>
<h3 id="31-dichannel-枚举">3.1 DIChannel 枚举</h3>
<p>DIChannel枚举定义了输入GPIO通道，从DI_CHANNEL_NUMBER_00到DI_CHANNEL_NUMBER_24，共25个通道。</p>
<pre><code class="language-cpp"><span class="hljs-keyword">enum</span> <span class="hljs-title class_">DIChannel</span> {
    DI_CHANNEL_NUMBER_00 = <span class="hljs-number">0</span>,   <span class="hljs-comment">//用于创建第00路IN对象</span>
    DI_CHANNEL_NUMBER_01,       <span class="hljs-comment">//用于创建第01路IN对象</span>
    <span class="hljs-comment">// ...</span>
    DI_CHANNEL_NUMBER_24        <span class="hljs-comment">//用于创建第24路IN对象</span>
};
</code></pre>
<h3 id="32-dochannel-枚举">3.2 DOChannel 枚举</h3>
<p>DOChannel枚举定义了输出GPIO通道，从DO_CHANNEL_NUMBER_00到DO_CHANNEL_NUMBER_11，共12个通道。</p>
<pre><code class="language-cpp"><span class="hljs-keyword">enum</span> <span class="hljs-title class_">DOChannel</span> {
    DO_CHANNEL_NUMBER_00 = <span class="hljs-number">0</span>,   <span class="hljs-comment">//用于创建第00路OUT对象</span>
    DO_CHANNEL_NUMBER_01,       <span class="hljs-comment">//用于创建第01路OUT对象</span>
    <span class="hljs-comment">// ...</span>
    DO_CHANNEL_NUMBER_11        <span class="hljs-comment">//用于创建第11路OUT对象</span>
};
</code></pre>
<h2 id="4-产品型号支持">4. 产品型号支持</h2>
<p>系统支持以下几种产品型号：</p>
<ul>
<li><strong>PRODUCT_MODEL_HTDAC101_R2100</strong>：支持全部DI和DO通道</li>
<li><strong>PRODUCT_MODEL_HTDAC070_C2300</strong>：支持DI-00到DI-03和DO-00到DO-03通道</li>
<li><strong>PRODUCT_MODEL_HT3568M_1000</strong>：支持全部DI和DO通道</li>
</ul>
<h2 id="5-使用示例">5. 使用示例</h2>
<h3 id="51-创建并使用输入gpio">5.1 创建并使用输入GPIO</h3>
<pre><code class="language-cpp"><span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&quot;GpioFactory.h&quot;</span></span>

<span class="hljs-comment">// 创建第0路输入GPIO</span>
BasicGpio* gpio = GpioFactory::<span class="hljs-built_in">createDI</span>(DIChannel::DI_CHANNEL_NUMBER_00);

<span class="hljs-comment">// 初始化GPIO</span>
<span class="hljs-keyword">if</span> (gpio-&gt;<span class="hljs-built_in">init</span>()) {
    <span class="hljs-comment">// 读取GPIO值</span>
    <span class="hljs-type">int</span> value;
    <span class="hljs-keyword">if</span> (gpio-&gt;<span class="hljs-built_in">getValue</span>(value)) {
        std::cout &lt;&lt; <span class="hljs-string">&quot;GPIO值为: &quot;</span> &lt;&lt; value &lt;&lt; std::endl;
    }
}

<span class="hljs-comment">// 释放资源</span>
<span class="hljs-keyword">delete</span> gpio;
</code></pre>
<h3 id="52-创建并使用输出gpio">5.2 创建并使用输出GPIO</h3>
<pre><code class="language-cpp"><span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&quot;GpioFactory.h&quot;</span></span>

<span class="hljs-comment">// 创建第0路输出GPIO</span>
BasicGpio* gpio = GpioFactory::<span class="hljs-built_in">createDO</span>(DOChannel::DO_CHANNEL_NUMBER_00);

<span class="hljs-comment">// 初始化GPIO</span>
<span class="hljs-keyword">if</span> (gpio-&gt;<span class="hljs-built_in">init</span>()) {
    <span class="hljs-comment">// 设置GPIO值为1</span>
    gpio-&gt;<span class="hljs-built_in">setValue</span>(<span class="hljs-number">1</span>);
}

<span class="hljs-comment">// 释放资源</span>
<span class="hljs-keyword">delete</span> gpio;
</code></pre>
<h3 id="53-通过gpio编号和方向创建gpio">5.3 通过GPIO编号和方向创建GPIO</h3>
<pre><code class="language-cpp"><span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&quot;GpioFactory.h&quot;</span></span>

<span class="hljs-comment">// 创建GPIO，编号为17，方向为输出</span>
BasicGpio* gpio = GpioFactory::<span class="hljs-built_in">createGpio</span>(<span class="hljs-number">17</span>, <span class="hljs-string">&quot;out&quot;</span>);

<span class="hljs-comment">// 初始化GPIO</span>
<span class="hljs-keyword">if</span> (gpio-&gt;<span class="hljs-built_in">init</span>()) {
    <span class="hljs-comment">// 设置GPIO值为0</span>
    gpio-&gt;<span class="hljs-built_in">setValue</span>(<span class="hljs-number">0</span>);
}

<span class="hljs-comment">// 释放资源</span>
<span class="hljs-keyword">delete</span> gpio;
</code></pre>
<h2 id="6-注意事项">6. 注意事项</h2>
<ol>
<li>使用GPIO前必须先调用init()方法进行初始化</li>
<li>使用完GPIO对象后，需要手动释放资源（delete操作）</li>
<li>不同产品型号支持的GPIO通道数量可能不同，请根据实际产品型号选择合适的通道</li>
<li>GPIO操作可能需要管理员权限，请确保应用程序有足够的权限</li>
<li>在多线程环境下使用GPIO时，需要注意线程安全问题</li>
</ol>
<h2 id="7-错误处理">7. 错误处理</h2>
<p>GPIO操作过程中可能出现的错误及处理方法：</p>
<ol>
<li><strong>初始化失败</strong>：检查GPIO编号是否正确，是否有足够的权限</li>
<li><strong>设置值失败</strong>：检查GPIO是否已初始化，方向是否为输出</li>
<li><strong>获取值失败</strong>：检查GPIO是否已初始化，方向是否为输入</li>
<li><strong>解析软链接失败</strong>：检查设备路径是否正确</li>
<li><strong>提取GPIO编号失败</strong>：检查设备路径格式是否正确</li>
</ol>
<h2 id="8-版本信息">8. 版本信息</h2>
<ul>
<li><strong>版本</strong>：1.0.0</li>
<li><strong>日期</strong>：2024-11-19</li>
<li><strong>作者</strong>：郑满乐</li>
<li><strong>版权</strong>：Copyright(C) 2024, HTDisplay. All Rights Reserved</li>
</ul>

            
            
        </body>
        </html>